// submit Utils
(function($) {
	$.onSubmit = function(data, klass) {
	};
	$.fn.onSubmit = function(options) {
		try {
			if (this.keepProcess) {
				alert("process ing.....");
				return false;
			}
			
			if (options.keepProcess != null || this.keepProcess == undefined)
				this.keepProcess = options.keepProcess;

			if (options.validate != null) {
				if(!showValidate($(this).attr("id"), options.validate, options.validateTitle))
                    return false;
			}
			
			if (options.confirmMessage) {
				if (!confirm(options.confirmMessage))
					return;
			}

			if (options.searchPush == true) {
				this.find("input:hidden").each(function() {
					var elementId = $(this).attr("id");
					var searchid = null;
					if (elementId) {
						searchId = "#search" + elementId.substring(0, 1).toUpperCase() + elementId.substring(1, elementId.length);
					}

					if ($(searchId).length > 0) {
						$(this).val($(searchId).val());
					}

				});
			}
			
			if (options.callpost != null) {
				var result = options.callpost();
				if (result == false)
					return result;
			}
			
			if (options.method != null) {
				$(this).find("#method").val(options.method);
			}

			if (options.frmmethod != null) {
				$(this).prop("method", options.frmmethod);
			}
			
			if (options.target != null) {
				$(this).attr({
						action : options.url,
						target : options.target
					}).submit();
			} else {
				$(this).attr({
					action : options.url,
					target : "_self"
				}).submit();
			}
			} catch (e){
				$.onSubmit.keepProcess = false;
				return false;
			}
		return true;
	};
})(jQuery);

//ajaxDefindSubmit
/**async:false만 지원 합니다. true시 리턴값을 받아올수 없으므로 async가 필요 한경우 일반적인 ajax 호출을 사용하세요. */
(function($) {
	$.ajaxDefindSubmit = function(data, klass) {
	};
	$.fn.ajaxDefindSubmit = function(options) {
		var _ajax_result;
		
		if (options.validate != null) {
			if(!showValidate($(this).attr("id"), options.validate, options.validateTitle))
                return false;
		}
		
		if (options.confirmMessage) {
			if (!confirm(options.confirmMessage))
				return false;
		}

		/*file 업로드때문에 아래 구문으로 대체
		$.ajax({
		    url: options.url,
		    async : false,
		    type: options.type,
		    timeout: 1000,
		    data: options.data,
		    dataType : "json",
		    error: function(xhr, status, error){
		    	//alert(xhr.responseText);
		    	_ajax_result = "error";
		    },
		    success: function(data){
		    	_ajax_result = data.result;
		    }
		});		
		return _ajax_result;		
		*/
		var _dataType = options.dataType == undefined ? "json" : options.dataType;
		var _frmType = options.type == undefined ? "post" : options.type;
		var _errorMsg = options.errorMsg == undefined ? "" : options.errorMsg;
		var _blockUI = options.blockUI == undefined ? true : options.blockUI;
		
		var _ajaxOptions = {
			url: options.url,
		    async : false,
		    type: _frmType,
		    timeout: 3000,
		    dataType : _dataType,
		    blockUI  : _blockUI,
		    beforeSend: function ( xhr ) {
		    	if(_blockUI){
		    		$(".ajaxloadingmodal").each(function() {
						$('.ajaxloadingmodal').show();
					});
		    	}
		    },
		    complete: function ( xhr ) {
		    	if(_blockUI){
		    		$(".ajaxloadingmodal").each(function() {
			    		$('.ajaxloadingmodal').fadeOut("slow");
					});
		    	}		    	
		    },
		    error: function(xhr, status, error){
		    	//alert(xhr.responseText);
		    	//return "error";
		    	if(_errorMsg != "" ){
		    		alert(_errorMsg);
		    	}

		    },
		    success: function(data){	    	
		    	if(_dataType.toLowerCase() == "html"){
		    		_ajax_result = data;
		    	}else{
		    		_ajax_result = data.result;	
		    	}
		    }
		};
		
		$(this).ajaxForm(_ajaxOptions).submit();
		$(this).ajaxFormUnbind();
		
		return _ajax_result;
	};
})(jQuery);

// checkbox Utils
(function($) {
	$.check = function(data, klass) {
	};

	$.extend($.check, {
		settings : {}
	});
	$.fn.check = function(target) {
		var $element = $(this);
		var checkedval = true;

		$element.unbind().click(function() {
			$(target +":not([disabled])").each(function() {

				if ($element.attr("id") == $(this).attr("id"))
					return true;
				
				checkedval = $element.attr("checked");
				
				if(checkedval == undefined)
					checkedval = false;
				
				$(this).attr("checked", checkedval);
				
				if ($element.attr("checked"))
					$(this).parents('tr:first').addClass("selected");
				else
					$(this).parents('tr:first').removeClass("selected");
			});
			this.blur();
		});

		$(target).each(function() {
			if ($element.attr("id") != $(this).attr("id")) {
				$(this).unbind().click(function() {
					var checked = $(this).attr("checked");
					var checkAll = true;
					if (checked) {
						$(target).each(function() {
							if (!$(this).attr("checked")) {
								checkAll = false;
								return false;
							}
						});
						$(this).parents('tr:first').addClass("selected");
					} else {
						checkAll = false;
						$(this).parents('tr:first').removeClass("selected");
					}
					$element.attr("checked", checkAll);
					this.blur();
				});
			}
		});
	};

})(jQuery);